package LeetCode._02_数组;

import java.util.HashMap;

public class _567_字符串的排列 {
    private HashMap<Character,Integer> need = new HashMap<>();
    private HashMap<Character,Integer> window = new HashMap<>();
    public boolean checkInclusion(String s1, String s2) {
        if(s1 == null || s2 == null || s1.length() == 0 || s2.length() == 0) return false;
        for (char c : s1.toCharArray()) {
            need.put(c,need.getOrDefault(c,0)+1);
        }
        int left = 0,right = 0;
        int count = 0;

        while(right<s2.length()){
            char c = s2.charAt(right);
            right++;
            if(need.containsKey(c)){
                window.put(c,window.getOrDefault(c,0)+1);
                if(window.get(c).equals(need.get(c))){
                    count++;
                }
            }

            while(right - left >= s1.length()){
                if(count == need.size()) return true;
                char d = s2.charAt(left);
                left++;
                if(need.containsKey(d)){
                    if(window.get(d).equals(need.get(d))){
                        count--;
                    }
                    window.put(d,window.get(d)-1);
                }
            }
        }
        return false;
    }
}
